<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="审核状态" prop="examine">
        <el-select
          v-model="form.examine"
          clearable
          placeholder="请选择"
          @change="setShowReason"
        >
          <el-option label="待审核" :value="0"></el-option>
          <el-option label="已通过" :value="1"></el-option>
          <el-option label="未通过" :value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="审核理由" :prop="showReason">
        <el-input v-model="form.reason" type="textarea"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/table'
  import {
    activityExamine,
    helpExamine,
    albumExamine,
    newsExamine,
  } from '@/api/management/management'
  export default {
    name: 'Examine',
    data() {
      return {
        form: {
          examine: '',
          reason: '',
        },
        rules: {
          examine: [
            { required: true, trigger: 'change', message: '请选择审核状态' },
          ],
          reason: [
            { required: true, trigger: 'blur', message: '请输入审核理由' },
          ],
        },
        //是否校验 审核理由
        showReason: '',
        title: '审核',
        dialogFormVisible: false,
        type: '',
      }
    },
    methods: {
      showEdit(row, type) {
        this.type = type
        this.form = Object.assign({}, row)
        this.dialogFormVisible = true
      },
      close() {
        //放开会出现 form被初始化

        // this.$refs['form'].resetFields()
        // this.form = this.$options.data().form
        this.dialogFormVisible = false
        // this.$emit('fetchData')
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            //判断 类型
            //0-互助类别，1-活动类别，2-相册类别，3-资讯类别
            if (this.type == 0) {
              helpExamine(this.form).then((res) => {
                if (res.code == 200) {
                  this.$baseMessage(res.data, 'success')
                } else {
                  this.$baseMessage('修改失败', 'error')
                }
              })
            } else if (this.type == 1) {
              activityExamine(this.form).then((res) => {
                if (res.code == 200) {
                  this.$baseMessage(res.data, 'success')
                } else {
                  this.$baseMessage('修改失败', 'error')
                }
              })
            } else if (this.type == 2) {
              albumExamine(this.form).then((res) => {
                if (res.code == 200) {
                  this.$baseMessage(res.data, 'success')
                } else {
                  this.$baseMessage('修改失败', 'error')
                }
              })
            }
            //置空
            this.$refs['form'].resetFields()
            this.dialogFormVisible = false
            this.$emit('fetchData')
            //获取页面初始值
            // this.form = this.$options.data().form
          } else {
            return false
          }
        })
      },
      setShowReason() {
        if (this.form.examine == 2) {
          this.showReason = 'reason'
        } else {
          this.showReason = ''
        }
      },
    },
  }
</script>
